let goodsDetail = {};
// 放大镜
goodsDetail.magnifyingClass = () => {

    let bigBox = $("#bigBox")[0];
    let moverBox = $("#moverBox")[0];
    let smallBox = $("#smallBox").children();
    let magnifyBox = $("#magnifyBox")[0];
    console.log(smallBox)
    // for循环绑事件
    for (let div of smallBox) {

        div.onclick = function () {
            bigBox.style.backgroundImage = getStyle(this, "backgroundImage");
            magnifyBox.style.backgroundImage = getStyle(this, "backgroundImage");
        }
    }

    bigBox.onmousemove = function (event) {

        let moveBoxWidth = moverBox.offsetWidth / 2;
        let moveBoxHeight = moverBox.offsetHeight / 2;
        let x = event.clientX - this.getBoundingClientRect().left - moveBoxWidth;
        let y = event.clientY - this.getBoundingClientRect().top - moveBoxHeight;
        $(magnifyBox).show();
        // changeMoverBox(event.offsetX, event.offsetY);****直接这样写不可以
        changeMoverBox(x, y);
        changemagnifyBox(x, y);
    }
    function changemagnifyBox(x, y) {
        magnifyBox.style.backgroundPosition = `-${x * 1}px -${y * 1}px`; // 字符串拼接
    }
    function changeMoverBox(x, y) {

        if (x <= 0) {

            x = 0;
        }
        if (x >= bigBox.offsetWidth - moverBox.offsetWidth) {

            x = bigBox.offsetWidth - moverBox.offsetWidth;
        }
        if (y <= 0) {

            y = 0;
        }
        if (y >= bigBox.offsetHeight - moverBox.offsetHeight) {

            y = bigBox.offsetHeight - moverBox.offsetHeight


        }

        moverBox.style.left = x + "px";
        moverBox.style.top = y + "px";
    }
}


/*
@ desc   数量加减
@ param 
@ date  2019/10/24
@author cyj
*/
goodsDetail.cuountControl = () => {
    let obj = $('.less,.add');
    let count = $('.optCount1').text();
    obj.on('click', (e) => {
        if ($(e.target).hasClass('less')) {
            if (count > 1) {
                count--;
                $(e.target).removeClass('on')
            } else {
                count = 1;
                $(e.target).addClass('on')
                alert("此商品一件起售")
            }

        } else {

            if (count >= 100) {
                count = 100;
                $(e.target).addClass('on')
                alert("已达购买上限")
            } else {
                count++;
                $(e.target).removeClass('on');
                $('.less').removeClass('on');

            }
        }
        $('.optCount1').text(count);
    })
}



$(() => {
    goodsDetail.magnifyingClass();
    goodsDetail.cuountControl();
})

